home *** CD-ROM | disk | FTP | other *** search
- <xsl:stylesheet version="1.0"
- xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
- >
-
- <xsl:output method="text"/>
-
- <xsl:variable name="theBuffer2">
- <xsl:for-each select="/dates/datesUnSorted/date[position() < 6]">
- <xsl:sort select="@value"/>
- <xsl:value-of select="concat(@value, ' ')"/>
- </xsl:for-each>
- </xsl:variable>
-
-
-
- <xsl:template match="/">
-
- <xsl:variable name="theResult">
- <xsl:call-template name="mergeSingleNode">
- <xsl:with-param name="theSiblings" select="/dates/datesUnSorted/date"/>
- <xsl:with-param name="siblPosition" select="6"/>
- <xsl:with-param name="sortPad" select="$theBuffer2"/>
- </xsl:call-template>
- </xsl:variable>
-
- <xsl:value-of select="$theResult"/>
- </xsl:template>
-
- <xsl:template name="mergeSingleNode">
- <xsl:param name="theSiblings" select="/.."/>
- <xsl:param name="siblPosition" select="0"/>
- <xsl:param name="sortPad" />
-
- <xsl:choose>
- <xsl:when test="$siblPosition > count($theSiblings)">
-
- <xsl:value-of select="concat('Result: ', $sortPad)"/>
- </xsl:when>
- <xsl:otherwise>
- <xsl:variable name="Last"
- select="string-length($sortPad) div 9"/>
- <xsl:variable name="argNumber"
- select="$theSiblings[$siblPosition]/@value"/>
-
- <xsl:variable name="mergePosition">
- <xsl:call-template name="binSearch">
- <xsl:with-param name="argNumber" select="$argNumber"/>
- <xsl:with-param name="sortedEls" select="$sortPad"/>
- <xsl:with-param name="First" select="1"/>
- <xsl:with-param name="Last" select="$Last"/>
- </xsl:call-template>
- </xsl:variable>
-
- <xsl:choose>
- <xsl:when test="$mergePosition > 0">
-
- <xsl:variable name="newSortPad">
- <xsl:if test="$mergePosition > 1">
- <xsl:value-of select="substring($sortPad,
- 10,
- ($mergePosition - 1) * 9)"/>
- </xsl:if>
-
- <xsl:value-of select="concat($argNumber, ' ')"/>
-
- <xsl:if test="$mergePosition != $Last">
- <xsl:value-of
- select="substring($sortPad,
- $mergePosition * 9 + 1,
- 9 * ($Last - $mergePosition)) "/>
- </xsl:if>
- </xsl:variable>
-
-
- <!-- Now the recursive call -->
-
- <xsl:call-template name="mergeSingleNode">
- <xsl:with-param name="theSiblings" select="$theSiblings"/>
- <xsl:with-param name="siblPosition" select="$siblPosition + 1"/>
- <xsl:with-param name="sortPad" select="$newSortPad"/>
- </xsl:call-template>
- </xsl:when>
- <xsl:otherwise>
-
- <!-- A recursive call with the same $sortPad-->
-
- <xsl:call-template name="mergeSingleNode">
- <xsl:with-param name="theSiblings" select="$theSiblings"/>
- <xsl:with-param name="siblPosition"
- select="$siblPosition + 1"/>
- <xsl:with-param name="sortPad" select="$sortPad"/>
- </xsl:call-template>
- </xsl:otherwise>
- </xsl:choose>
- </xsl:otherwise>
- </xsl:choose>
-
- </xsl:template>
-
- <xsl:template name="binSearch">
- <xsl:param name="argNumber" select="-Infinity"/>
- <xsl:param name="sortedEls" />
- <xsl:param name="First" select="Infinity"/>
- <xsl:param name="Last" select="0"/>
-
- <xsl:choose>
- <xsl:when test="$First > $Last">
- <xsl:value-of select="$Last"/>
- </xsl:when>
- <xsl:otherwise>
- <xsl:variable name="Mid"
- select="floor(($First + $Last) div 2)"/>
- <xsl:variable name="midElement"
- select="substring($sortedEls,
- 9 * ($Mid - 1) + 1,
- 8)"/>
-
- <xsl:choose>
- <xsl:when test="$argNumber = $midElement">
- <xsl:value-of select="$Mid"/>
- </xsl:when>
- <xsl:when test="$argNumber < $midElement">
- <xsl:choose>
- <xsl:when test="$First < $Last">
- <xsl:call-template name="binSearch">
- <xsl:with-param name="argNumber" select="$argNumber"/>
- <xsl:with-param name="sortedEls" select="$sortedEls"/>
- <xsl:with-param name="First" select="$First"/>
- <xsl:with-param name="Last" select="$Mid - 1"/>
- </xsl:call-template>
- </xsl:when>
- <xsl:otherwise><!-- First = Last and the comparison is lt -->
- <xsl:value-of select="$First - 1"/>
- </xsl:otherwise>
- </xsl:choose>
- </xsl:when>
- <xsl:when test="$argNumber > $midElement">
- <xsl:choose>
- <xsl:when test="$First < $Last">
- <xsl:call-template name="binSearch">
- <xsl:with-param name="argNumber" select="$argNumber"/>
- <xsl:with-param name="sortedEls" select="$sortedEls"/>
- <xsl:with-param name="First" select="$Mid + 1"/>
- <xsl:with-param name="Last" select="$Last"/>
- </xsl:call-template>
- </xsl:when>
- <xsl:otherwise><!-- First = Last and the comparison is gt -->
- <xsl:value-of select="$Last"/>
- </xsl:otherwise>
- </xsl:choose>
- </xsl:when>
- <xsl:otherwise>
- ERRROR
- </xsl:otherwise>
-
- </xsl:choose>
- </xsl:otherwise>
-
-
- </xsl:choose>
- </xsl:template>
-
- </xsl:stylesheet>
-